package com.ysg.widget.dialog;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.core.content.FileProvider;
import androidx.fragment.app.FragmentActivity;
import com.ysg.R;
import com.ysg.base.BaseDialog;
import com.ysg.constant.FileConstant;
import com.ysg.http.DownLoadManager;
import com.ysg.http.data.download.ProgressCallBack;
import com.ysg.http.data.entity.mine.VersionEntity;
import com.ysg.utils.YPermissionUtil;
import com.ysg.utils.YStringUtil;
import java.io.File;

/**
 * 版本更新
 */
public class VersionDialog extends BaseDialog {

    private VersionEntity data;
    public VersionDialog(Context context, VersionEntity data) {
        super(context);
        this.data = data;
    }

    private View tvCancel, tvConfirm;
    private TextView tvVersionName, tvVersionContent;
    private SeekBar seekBar;

    @Override
    public int getLayoutId() {
        return R.layout.dialog_version;
    }

    public void initView() {
        setCancelable(false);
        setCanceledOnTouchOutside(false);

        tvVersionName = findViewById(R.id.tvVersionName);
        tvVersionContent = findViewById(R.id.tvVersionContent);
        tvCancel = findViewById(R.id.tvCancel);
        tvConfirm = findViewById(R.id.tvConfirm);
        seekBar = findViewById(R.id.seekBar);
    }


    @Override
    public void initData() {
        tvVersionName.setText(data.getVername());
        tvVersionContent.setText(data.getContent());
        if (YStringUtil.eq(1, data.getUpdatestate())){
            tvCancel.setVisibility(View.GONE);
        }else{
            tvCancel.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void initListener() {
        tvCancel.setOnClickListener((v)->dismiss());

        tvConfirm.setOnClickListener((v)-> download(data.getUrl()));
    }

    private void download(String apkUrl){
        seekBar.setVisibility(View.VISIBLE);
        tvConfirm.setClickable(false);

        YPermissionUtil.requestStorage((FragmentActivity) mContext,"申请存储权限：用于app版本更新", new YPermissionUtil.OnPermissionListener() {
            @Override
            public void onSuccess() {
                DownLoadManager.getInstance().load(apkUrl, false, new ProgressCallBack(FileConstant.apk_dir, FileConstant.apk_file) {
                    @Override
                    public void onSuccess(Object o) {
                        Log.e("下载成功", "下载成功");
                        String apkPath = getContext().getExternalFilesDir(null).getPath() + File.separator + FileConstant.apk_dir + "/"+ FileConstant.apk_file;
                        File file = new File(apkPath);
                        installApk(file);
                        tvConfirm.setClickable(true);
                    }

                    @Override
                    public void progress(long progress, long total) {
                        long p = (progress*100) / total;
                        Log.e("下载progress", "progress:"+p);
                        seekBar.setProgress((int)p);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("下载onError", "onError");
                        tvConfirm.setClickable(true);
                    }
                });
            }

            @Override
            public void onError() {
            }
        });

    }

    private void installApk(File file){
        try {
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                uri = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".version.fileProvider", file);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            } else {
                uri = Uri.fromFile(file);
            }

            intent.setDataAndType(uri,"application/vnd.android.package-archive");
            getContext().startActivity(intent);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


}
